/*
 * @Author: jqgao2
 * @Date: 2022-10-25 09:46:27
 * @LastEditTime: 2022-11-06 19:58:21
 * @Description: 脚手架配置
 * @FilePath: \vue2-ele\vue.config.js
 * @LastEditors: jqgao2
 */
const { defineConfig } = require("@vue/cli-service");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const isProduction = process.env.NODE_ENV === "production";
const path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = defineConfig({
  productionSourceMap: false,
  transpileDependencies: true,
  // 开发环境服务器代理
  devServer: {
    host: "0.0.0.0",
    open: true,
    proxy: {
      "/dev-api": {
        changeOrigin: true,
        ws: true,
        target: "http://10.5.123.8",
      },
    },
  },
  configureWebpack: (config) => {
    // 生产环境相关配置
    if (isProduction) {
      //gzip压缩
      const productionGzipExtensions = ["html", "js", "css"];
      config.plugins.push(
        new CompressionWebpackPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
          threshold: 10240, // 只有大小大于该值的资源会被处理 10240
          minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
          deleteOriginalAssets: false, // 删除原文件
        })
      );
    }
  },
  chainWebpack(config) {
    config.module.rule("svg").exclude.add(resolve("src/assets/svg")).end();
    //设置svg-sprite-loader处理icons目录中的svg
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/assets/svg"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({ symbolId: "icon-[name]" })
      .end();
  },
});
